import axios from "@/api/api_config"
import router from "@/router"
//import * as jwt from 'jsonwebtoken'
import {jwtDecode} from 'jwt-decode'
//登录
export const loginUser = async(login)=>{
	return await axios.post('Users/auth',login)
}

const key = 'tokenAnt'
//从浏览器本地存储获取token值
export const getToken=()=>{
	return localStorage.getItem(key)
}
//清除token
export const logOut=()=>{
	//localStorage.clear()
	localStorage.removeItem(key)
	router.replace('/login')
}
//设置token,不需要，因为只有用到一次(登录)

//pnpm add jsonwebtoken安装  vue/cli>=5时
//pnpm add node-polyfill-webpack-plugin

//检查token过期时间
export const isTokenFromLocalStorageVaild=()=>{
	const token = localStorage.getItem(key)
	if(!token)return false
	//const decoded = jwt.decode(token)//解析 解码
	const decoded =  jwtDecode(token)
	//时间戳分为10位(秒级)和13位(毫秒级)的
	const dateNow = Date.now()
	const expiresAt = decoded.exp*1000
	console.log(decoded)
	return dateNow<=expiresAt
}